package com.castle.fortress.admin.flowable.delegate;

import com.castle.fortress.admin.flowable.entity.FlowableAppEntity;
import com.castle.fortress.admin.flowable.service.FlowableAppService;
import org.flowable.common.engine.api.delegate.Expression;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.springframework.beans.factory.config.BeanDefinition.SCOPE_PROTOTYPE;

/**
 * 抄送人服务
 * @author castle
 */
@Scope(SCOPE_PROTOTYPE)
@Service
public class FortressRecipientsDelegate implements JavaDelegate {
    @Autowired
    private FlowableAppService flowableAppService;
    @Autowired
    private RuntimeService runtimeService;
    //抄送的部门 数据用逗号隔开
    private Expression depts;
    //抄送的人员 数据用逗号隔开
    private Expression users;

    @Override
    public void execute(DelegateExecution delegateExecution) {
        System.out.println("--------FortressRecipientsDelegate--------");
        List<FlowableAppEntity> list = flowableAppService.list();
        System.out.println(list);
        String csData = "";
        if(depts!=null){
            String toDepts = (String) depts.getValue(delegateExecution);
            System.out.println("toDepts:"+toDepts);
            csData+=toDepts+",";
        }
        //系统预设的抄送人
        if(users!=null){
            String toUsers = (String) users.getValue(delegateExecution);
            System.out.println("toUsers:"+toUsers);
            csData+=toUsers+",";
        }
        //发起人自选的抄送人
        if(delegateExecution.hasVariable(delegateExecution.getCurrentActivityId())){
            System.out.println("发起人自选的抄送人:"+delegateExecution.getVariable(delegateExecution.getCurrentActivityId()));
            csData+=delegateExecution.getVariable(delegateExecution.getCurrentActivityId())+",";
        }
        //审批人自选的
        if(delegateExecution.hasVariable(delegateExecution.getCurrentActivityId()+"_cs")
            && !"nobody".equals(delegateExecution.getVariable(delegateExecution.getCurrentActivityId()+"_cs"))
        ){
            System.out.println("审批人自选的抄送人:"+delegateExecution.getVariable(delegateExecution.getCurrentActivityId()+"_cs"));
            csData+=delegateExecution.getVariable(delegateExecution.getCurrentActivityId()+"_cs")+",";
        }
        System.out.println("delegateExecution:"+delegateExecution);
        System.out.println("reject----------");
        //将抄送人放到参数表中
        if(runtimeService.hasVariable(delegateExecution.getId(),"_castle_flowable_cs_data")){
            runtimeService.setVariable(delegateExecution.getId(),"_castle_flowable_cs_data",runtimeService.getVariable(delegateExecution.getId(),"_castle_flowable_cs_data")+csData);
        }else{
            runtimeService.setVariable(delegateExecution.getId(),"_castle_flowable_cs_data",csData);
        }
    }
}
